package com.javatest.springboot3minio.utils;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.RemoveBucketArgs;
import io.minio.messages.Bucket;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created with IntelliJ IDEA. Description:
 *
 * @author lzy
 * @since 2022/10/14 上午11:36
 */
public class MinioBucketUtil {
  private static final Logger LOGGER = LoggerFactory.getLogger(MinioBucketUtil.class);
  private static MinioClient MINIO_CLIENT;

  @Autowired
  public void setMinioClient(MinioClient minioClient) {
    MINIO_CLIENT = minioClient;
  }

  public static MinioBucketUtil getBucketUtilSingleton() {
    return MinioBucketUtilHolder.INSTANCE;
  }

  /**
   * Bucket exists boolean.
   *
   * @param bucketName the bucket name
   * @return the boolean
   */
  public static boolean bucketExists(String bucketName) {
    boolean found = false;
    try {
      found = MINIO_CLIENT.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    } catch (Exception e) {
      LOGGER.error("[Minio] Check bucket exists meet error.", e);
    }

    return found;
  }

  /**
   * 创建bucket，如果已经存在同名的bucket，则不会进行覆盖
   *
   * @param bucketName the bucket name
   * @return the boolean
   */
  public static boolean createBucket(String bucketName) {
    if (bucketExists(bucketName)) {
      return true;
    }

    try {
      MINIO_CLIENT.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
    } catch (Exception e) {
      LOGGER.error("[Minio] Make bucket exists meet error.", e);
      return false;
    }

    return true;
  }

  /**
   * Remove bucket boolean.
   *
   * @param bucketName the bucket name
   * @return the boolean
   */
  public static boolean removeBucket(String bucketName) {
    if (!bucketExists(bucketName)) {
      return true;
    }

    try {
      MINIO_CLIENT.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    } catch (Exception e) {
      LOGGER.error("[Minio] Remove bucket exists meet error.", e);
      return false;
    }

    return true;
  }

  /**
   * Get all buckets list.
   *
   * @return the list
   */
  public static List<Bucket> getAllBuckets() {
    try {
      return MINIO_CLIENT.listBuckets();
    } catch (Exception e) {
      LOGGER.error("[Minio] List buckets meet error.", e);
    }

    return Collections.EMPTY_LIST;
  }

  /**
   * List bucket names list.
   *
   * @return the list
   */
  public static List<String> listBucketNames() {
    final List<Bucket> allBuckets = getAllBuckets();
    if (Objects.isNull(allBuckets)) {
      return Collections.EMPTY_LIST;
    }

    return allBuckets.stream().map(Bucket::name).collect(Collectors.toList());
  }

  private static class MinioBucketUtilHolder {
    private static final MinioBucketUtil INSTANCE = new MinioBucketUtil();
  }
}
